package com.qingfeng.medical.controller.admin.message;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.handler.GeneralAuthException;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.auth.domain.entity.SysUser;
import com.qingfeng.medical.biz.auth.service.SysUserService;
import com.qingfeng.medical.biz.information.domain.vo.UserInfoDiseaseVo;
import com.qingfeng.medical.biz.message.domain.dto.DiseaseConsultationSaveDTO;
import com.qingfeng.medical.biz.message.domain.entity.DiseaseConsultation;
import com.qingfeng.medical.biz.message.domain.enums.CustomerTypeEnum;
import com.qingfeng.medical.biz.message.domain.enums.ReadStatusEnum;
import com.qingfeng.medical.biz.message.service.DiseaseConsultationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 病情咨询表
 *
 * @author 清风学Java
 * @date 2023-04-14 14:47:16
 */
@Api(tags = "病情咨询表管理")
@RestController
@RequestMapping("/admin/msg/diseaseConsultation")
@Validated
public class DiseaseConsultationController extends BaseController {

    @Autowired
    private DiseaseConsultationService diseaseConsultationService;
    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('bnt.disease.user')")
    @ApiOperation(value = "查询咨询用户列表", notes = "查询咨询用户列表")
    @GetMapping("/user/list")
    @Log(title = "病情咨询管理", businessType = BusinessType.SELECT, message = "查询咨询用户列表")
    public Result<List<UserInfoDiseaseVo>> userList() {
        List<UserInfoDiseaseVo> userInfoDiseaseVoList = diseaseConsultationService.userList(getUserId());
        return Result.ok(userInfoDiseaseVoList);
    }

    @PreAuthorize("hasAuthority('bnt.disease.list')")
    @ApiOperation(value = "病情咨询信息列表", notes = "病情咨询信息列表")
    @GetMapping("/list/{userId}")
    @Log(title = "病情咨询管理", businessType = BusinessType.SELECT, message = "病情咨询信息列表")
    @Transactional(rollbackFor = GeneralAuthException.class)
    public Result<List<DiseaseConsultation>> diseaseList(@PathVariable("userId") Long userId) {
        List<DiseaseConsultation> diseaseConsultationList = diseaseConsultationService.list(
                new LambdaQueryWrapper<DiseaseConsultation>()
                        .eq(DiseaseConsultation::getUserId, userId)
                        .eq(DiseaseConsultation::getDoctorId, getUserId())
        );

        if (CollUtil.isNotEmpty(diseaseConsultationList)) {
            diseaseConsultationList.stream()
                    .filter(d -> d.getCustomerType().equals(CustomerTypeEnum.PATIENT))
                    .forEach(d -> d.setReadStatus(ReadStatusEnum.READ));

            // 更新
            diseaseConsultationService.updateBatchById(diseaseConsultationList);
        }

        return Result.ok(diseaseConsultationList);
    }

    @PreAuthorize("hasAuthority('bnt.disease.save')")
    @ApiOperation(value = "保存病情咨询表信息", notes = "保存病情咨询表信息")
    @PostMapping
    @Log(title = "病情咨询管理", businessType = BusinessType.INSERT, message = "保存病情咨询表信息")
    public Result saveDiseaseConsultation(@RequestBody @Validated DiseaseConsultationSaveDTO diseaseConsultationSaveDTO){
        diseaseConsultationService.saveDiseaseConsultation(diseaseConsultationSaveDTO);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.disease.doctor')")
    @ApiOperation(value = "查询医师信息", notes = "查询医师信息")
    @GetMapping("/doctor/info")
    @Log(title = "病情咨询管理", businessType = BusinessType.SELECT, message = "查询医师信息")
    public Result<SysUser> doctorInfo() {
        return Result.ok(sysUserService.getById(getUserId()));
    }

}
